<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

    <title> - 登录</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/animate.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/login.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html" />
    <![endif]-->
    <script>
        if (window.top !== window.self) {
            window.top.location = window.location;
        }
    </script>

</head>

<body class="signin">
    <div class="signinpanel">
        <div class="row">
            <div class="col-sm-12">
                <form id="form" method="post">
                    <h4 class="no-margins">登录：</h4>
                    <p class="m-t-md">登录到H+后台主题UI框架</p>
                    <input type="text" id="name" name="name" class="form-control uname" placeholder="用户名" />
                    <input type="password" id="password" name="password" class="form-control pword m-b" placeholder="密码" />
                    <a href="">忘记密码了？</a>
                </form>
                <button class="btn btn-success btn-block" onclick="submitForm()">登录</button>
            </div>
        </div>
        <div class="signup-footer">
            <div class="pull-left">
                &copy; hAdmin
            </div>
        </div>
    </div>
    
    
    <script src="${pageContext.request.contextPath}/js/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
	<script src="${pageContext.request.contextPath}/layer/layer.js" type="text/javascript" charset="utf-8"></script>
	<script src="${pageContext.request.contextPath}/js/mylayer.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		function submitForm() {
			var name = $("#name").val();
			var password = $("#password").val();
			
			// 1、首先在前台通过正则表达式验证格式对不对
			// 用户名必须满足： 4-8位的数字或者字母
			var pattern = /^[0-9a-zA-Z]{4,8}$/;
			//if (!pattern.test(name)) {
			if (pattern.test(name) == false) {
				mylayer.errorMsg("格式非法，请输入 4-8位的数字或者字母");
				return;
			}
			
			// 2、格式对的情况下，再通过ajax请求后台完成登录
			$.post(
				"${pageContext.request.contextPath}/user/login.action",
				// 把form表单里面的数据，封装成json格式
				// name: zhangsan
				// password: 123	
				$("#form").serialize(),
				function(result) {
					console.log(result);
					//if(result.code == 1) {
					if(result.success) {
						mylayer.successUrl(result.msg, "${pageContext.request.contextPath}/index.html");
					} else {
						mylayer.errorMsg();
					}
				},
				"json"
			);
			
		}
	</script>
</body>

</html>
